<? 
$key = $_GET['key'];
	list($smokedperday,$costperpack,$day,$month,$year,$icon) = $db->fetch_row($db->query("SELECT smokedperday,costperpack,day,month,year,icon FROM smokefree WHERE id='$key'"));

// create some more variables
$today = time();
$quitdate = mktime(0,0,1,$month,$day,$year);
$readablequitdate = date("M j Y",$quitdate);
$daysquit = abs(((($today - $quitdate)/60)/60)/24);
if($daysquit>=1){
	$smokefree .= floor($daysquit)." days and ";
}
$hoursquit = floor((((abs($today - $quitdate)/60)/60)/24-floor($daysquit))*24);
$smokefree .= $hoursquit." hours";
$moneysaved = abs(round($smokedperday/20 * $costperpack * $daysquit,2));
$cigsavoided = abs(round($smokedperday * $daysquit));

// tell the browser we are an image.
header ("Content-type: image/png");

// create an image to work with or fail horribly.
$im = @imagecreatetruecolor($x, $y) or die("Cannot Initialize new image");

// Create the colors we will work with.
$red = imagecolorallocate($im,255,0,0);
$white = imagecolorallocate($im,255,255,255); 
$black = imagecolorallocate($im,0,0,0);

// start processing the image.
imagefill($im,0,0,$white); // make the whole image white.
imageline($im,0,0,$x,0,$black);
imageline($im,0,0,0,$y,$black);
imageline($im,0,$y-1,$x,$y-1,$black);
imageline($im,$x-1,0,$x-1,$y-1,$black);

// work with the icon...
if(!is_file($icon)){
	$icon = "./resources/icons/".$defaulticon;
}
list($iconwidth, $iconheight, $icontype, $iconattr) = getimagesize($icon);
$iconim = imagecreatefrompng($icon);

$toppadding = ($y-$iconheight)/2;

imagecopy($im,$iconim,4,$toppadding,0,0,$iconwidth,$iconheight);

// add the text/verbage.
if(mktime(0,0,1,$month,$day,$year) > time()){
	// differentiate between 'has quit' and 'will quit'
	imagestring($im, 5, $iconwidth+10, 5,  "I will quit smoking in...",$red);
	imagestring($im, 3, $iconwidth+10, 23, $smokefree, $black);
	imagestring($im, 3, $iconwidth+10, 40, "Spending \$$moneysaved on $cigsavoided Cigarettes.", $black);	
}else{
	imagestring($im, 5, $iconwidth+10, 5,  "I have been smoke free for...",$red);
	imagestring($im, 3, $iconwidth+10, 23, $smokefree, $black);
	imagestring($im, 3, $iconwidth+10, 40, "\$$moneysaved saved and $cigsavoided Cigarettes avoided!", $black);
}

imagepng($im); // push the image to the browser
imagedestroy($im); // destroy it out of memory.
?> 